package grok;

import com.google.common.collect.Lists;
import io.krakens.grok.api.Grok;
import io.krakens.grok.api.GrokCompiler;
import io.krakens.grok.api.Match;
import org.junit.Before;
import org.junit.Test;

import java.util.List;
import java.util.Map;

public class GrokDemo {

  static GrokCompiler grokCompiler = GrokCompiler.newInstance();

  @Before
  public void before() {
    // 进行注册, registerDefaultPatterns()方法注册的是Grok内置的patterns
    grokCompiler.registerDefaultPatterns();
  }

  @Test
  public void test() {
        /*
         传入自定义的pattern, 会从已注册的patterns里面进行配对, 例如: TIMESTAMP_ISO8601:timestamp1, TIMESTAMP_ISO8601在注册的
         patterns里面有对应的解析格式, 配对成功后, 会在match时按照固定的解析格式将解析结果存入map中, 此处timestamp1作为输出的key
          */
    Grok grok = grokCompiler.compile("%{TIMESTAMP_ISO8601:timestamp1}%{SPACE}%{WORD:location}"
            + ".%{WORD:level}%{SPACE}%{IP:ip}%{SPACE}%{MONTH:month}");
    String logMsg = "2018-08-23 02:56:53 Local7.Info 171.8.79.214 Aug Aug  23 02:56:53 2018 "
            + "S7506E-A %%10OSPF/6/ORIGINATE_LSA(t): OSPF TrapIDpID1.3.6.1.2.1..1.2.1.14.16.2.1.2.12ospfOriginateLsa: "
            + "Originate new LSA AreaId 0.0.0.0 LsdbType 5 LsdbLsid id 192.168.17.16Lsd LsdbRouterId Id 192.168.250.254"
            + " Rou Router er 192.168.250.254.";
    // 通过match()方法进行匹配, 对log进行解析, 按照指定的格式进行输出
    Match grokMatch = grok.match(logMsg);
    // 获取结果
//    Map<String, Object> resultMap = grokMatch.capture();
//    System.out.println(resultMap);
  }

  @Test
  public void test1() {
    String src = "[2021-03-02 18:53:44:728] [http-nio-8080-exec-1] INFO com.ycy.controller.ElkLogController   - hello word 1";
    String pattern = "\\[%{TIMESTAMP_ISO8601:logcreatetime}]\\s*\\[%{DATA:thread}\\]\\s*%{LOGLEVEL:level}\\s*%{GREEDYDATA:message}";
    Grok grok = this.grokCompiler.compile(pattern);
    Match grokMatch = grok.match(src);
    // 获取结果
//    Map<String, Object> resultMap = grokMatch.capture();
//    System.out.println(resultMap);
  }

  @Test
  public void test2() {
    String src = "[2021-03-02 18:53:44:728] [http-nio-8080-exec-1] INFO com.ycy.controller.ElkLogController   - hello word 1"
            + "S7506E-A %%10OSPF/6/ORIGINATE_LSA(t): OSPF TrapIDpID1.3.6.1.2.1..1.2.1.14.16.2.1.2.12ospfOriginateLsa: "
            + "Originate new LSA AreaId 0.0.0.0 LsdbType 5 LsdbLsid id 192.168.17.16Lsd LsdbRouterId Id 192.168.250.254"
            + " Rou Router er 192.168.250.254.";
    String pattern = "\\[%{TIMESTAMP_ISO8601:logcreatetime}] \\[%{DATA:thread}\\] %{LOGLEVEL:level} %{GREEDYDATA:message}";
    Grok grok = this.grokCompiler.compile(pattern);
    Match grokMatch = grok.match(src);
    // 获取结果
//    Map<String, Object> resultMap = grokMatch.capture();
//    System.out.println(resultMap);
  }

  @Test
  public void test3() {
    test();
    List<Long> list = Lists.newArrayList();
    for (int j = 0; j < 10; j++) {
      Long cur = System.currentTimeMillis();
      for (int i = 0; i < 10000; i++) {
        test2();
      }
      long x = System.currentTimeMillis() - cur;
      System.out.println(x);
      list.add(x);
    }

    long sum = 0;
    for (Long aLong : list) {
      sum += aLong;
    }
    System.out.println("总耗时 " + sum);
    System.out.println("平均   " + sum / 10);
    System.out.println("e/s    " + (100000) * 1000 / sum);

    /*
    * 1168
840
716
782
725
798
739
745
751
763
总耗时 8027
平均   802
e/s    12457*/

  }

}
